package com.fyzk.activity.lottery.basketball.prizeoptimization;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.lottery.shuzicai.InfoDetailActivity;
import com.fyzk.activity.lottery.zc.tz_dialog.ZCBF_infor;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.net.Constants;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.JCCodeTranslate;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.utils.jc.JZGroupUtil;
import com.fyzk.utils.jc.JZHGGroup;
import com.fyzk.utils.jc.StringArrayObj;
import com.fyzk.view.POASView;
import com.tyjc.lottery.R;
import com.tyjc.prizeOptimize.ItemSelect;
import com.tyjc.prizeOptimize.MatchSelect;
import com.tyjc.prizeOptimize.Optimize;
import com.tyjc.prizeOptimize.OptimizeException;
import com.tyjc.prizeOptimize.OptimizeType;
import com.tyjc.prizeOptimize.OptimizedMatch;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by miao on 2017/11/22.
 * 奖金优化
 */
public class JZPrizeOptimizationActivity extends JCBasePayActivity {

    int zhushu;
    int muti = 1;
    int amount = 0;
    int[] chuanguan;
    int lotteryId;

    POASView asv_money;
    POASView po_muti;
    TextView txt_amount;
    ListView po_result;

    View cover_view;

    LinearLayout ll_progress;

    OptimizeType optimizeType = OptimizeType.AVE;
    PoResultListAdapter poResultListAdapter;
    RadioGroup rgp_po_type;
    private String prizeMinStr;

    boolean canHotCold = false;

    RadioButton po_hot;
    RadioButton po_cold;

    private final int MAXPO = 500000;

    private boolean poing = false;

    private PrizeAsyncTask prizeAsyncTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.po_instruction).setVisibility(View.VISIBLE);
        findViewById(R.id.zhifu_top_hemai).setVisibility(View.GONE);
        findViewById(R.id.zhifu_bottom_tv_clear).setVisibility(View.GONE);
        findViewById(R.id.zhifu_bottom_tv_zhushu).setVisibility(View.GONE);
        findViewById(R.id.zhifu_bottom_tv_bei).setVisibility(View.GONE);
        LinearLayout ll_betmsg = (LinearLayout) findViewById(R.id.ll_betmsg);
        txt_amount = (TextView) findViewById(R.id.zhifu_bottom_tv_money);
        ll_betmsg.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        Intent intent = getIntent();

        String chuanguan_str = intent.getStringExtra("chuanguan");
        if (chuanguan_str.contains("串")) {
            String[] chuanguans = chuanguan_str.split(",");
            chuanguan = new int[chuanguans.length];
            int index = 0;
            for (String str : chuanguans) {
                int chuanguan_int = Integer.parseInt(str.substring(0, str.length() - 2));
                chuanguan[index] = chuanguan_int;
                index++;
            }
        } else {
            chuanguan = new int[]{1};
        }

        canHotCold = (chuanguan.length == 1 && infos.size() == chuanguan[0]);
        zhushu = intent.getIntExtra("zhushu", 1);
        lotteryId = intent.getIntExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils.JZSPF_LOTTERY_ID_INT);
        prizeMinStr = intent.getStringExtra("minprize");
        poResultListAdapter = new PoResultListAdapter();
        po_result.setAdapter(poResultListAdapter);
        prizeo();
        asv_money.setOnValueChangedLisener(null);
        asv_money.setValue(zhushu * 2);
        asv_money.setMin(zhushu * 2);
        asv_money.setRightText("米");
        asv_money.setOnValueChangedLisener(onValueChangedLisener);
        amount = zhushu * 2;
        txt_amount.setText("总彩金 " + amount + " 米");
        po_cold.setEnabled(canHotCold);
        po_hot.setEnabled(canHotCold);
    }

    public View initCenterView() {
        View view = LayoutInflater.from(this).inflate(R.layout.jz_zhifu_prizeoptimization, null);
        asv_money = view.findViewById(R.id.po_money);
        cover_view = view.findViewById(R.id.cover_view);
        cover_view.setOnClickListener(this);
        asv_money.setUnit(2);
        asv_money.setMax(MAXPO);
        po_result = view.findViewById(R.id.po_result);
        ll_progress = view.findViewById(R.id.ll_progress);
        po_muti = view.findViewById(R.id.po_muti);
        po_muti.setDesVisble(View.VISIBLE);
        po_muti.setLeftText("投");
        po_muti.setRightText("倍");
        po_muti.setOnValueChangedLisener(onMutiValueChangedLisener);
        rgp_po_type = view.findViewById(R.id.rgp_po_type);
        po_hot = ((RadioButton) view.findViewById(R.id.po_hot));
        po_cold = ((RadioButton) view.findViewById(R.id.po_cold));

        rgp_po_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.po_ave:
                        optimizeType = OptimizeType.AVE;
                        break;
                    case R.id.po_cold:
                        optimizeType = OptimizeType.COLD;
                        break;
                    case R.id.po_hot:
                        optimizeType = OptimizeType.HOT;
                        break;
                }
                prizeo();
            }
        });
        return view;
    }

    private void prizeo() {
        prizeAsyncTask = new PrizeAsyncTask();
        prizeAsyncTask.execute(zhushu);
    }

    @Override
    protected void parserMessage(Message msg) {
        super.parserMessage(msg);
        switch (msg.what) {
            case Constants.ERROR_HANDLER:
                showToast(msg.obj.toString());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    ArrayList<OptimizedMatch> optimizeMethod = new ArrayList<>();

    class PrizeAsyncTask extends AsyncTask<Integer, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            optimizeMethod.clear();
            visbleMaps.clear();
            poResultListAdapter.notifyDataSetChanged();
            ll_progress.setVisibility(View.VISIBLE);
            poing = true;
            safePoing();
        }


        @Override
        protected String doInBackground(Integer... util) {
            List<MatchSelect> matchSelects = new ArrayList<>();
            for (JCInfo info : infos) {
                Iterator<String> keys = info.btnHGPojo.keySet().iterator();
                List<ItemSelect> itemSelects = new ArrayList<>();
                MatchSelect matchSelect = new MatchSelect(info.getMatchNum(), itemSelects);
                while (keys.hasNext()) {
                    String key = keys.next();
                    JCInfo.SPInfo spInfo = info.btnHGPojo.get(key);
                    if (spInfo.isSel) {
                        ItemSelect item1 = new ItemSelect(info.getMatchNum(), info.getOfficialWeekDay(), info.getHomeTeam(), info.getAwayTeam(),
                                spInfo.codeValue, key, new
                                BigDecimal(spInfo.spValue), info.getHandicap());
                        itemSelects.add(item1);
                    }
                }
                matchSelects.add(matchSelect);
            }
            StringBuffer stringBuffer = new StringBuffer();
            optimizeMethod.clear();
            try {
                List<OptimizedMatch> optimizeMethod_compute = new Optimize().optimizeMethod(matchSelects, zhushu, optimizeType, chuanguan);
                for (OptimizedMatch optimizedMatch : optimizeMethod_compute) {
                    if (optimizedMatch.getMultiple() > 0) {
                        optimizeMethod.add(optimizedMatch);
                    }
                }
            } catch (OptimizeException e) {
                Message message = mHandler.obtainMessage();
                message.what = Constants.ERROR_HANDLER;
                message.obj = e.getMessage();
                mHandler.sendMessage(message);
            }
            for (OptimizedMatch optimizedMatch : optimizeMethod) {
                StringBuffer teams = new StringBuffer();
                for (ItemSelect select : optimizedMatch.getItemSelects()) {
                    teams.append(select.getHomeTeam() + "|" + JCCodeTranslate.getInstance().getCodeTranslate(select.getKey()) + "\n");
                }
                optimizedMatch.setTeams(teams.substring(0, teams.length() - 1).toString());
            }

            return stringBuffer.toString();
        }

        @Override
        protected void onPostExecute(String o) {
            super.onPostExecute(o);
            poing = false;
            poResultListAdapter.notifyDataSetChanged();
            setPrizeAndAmtView();
            ll_progress.setVisibility(View.GONE);
            safePoing();
        }
    }

    Map<String, VisibleLl> visbleMaps = new HashMap<>();

    class VisibleLl {
        int visible;
    }

    /**
     * 保证奖金优化进行中的时候不能进行其他操作
     */
    private void safePoing() {
        if (poing) {
            cover_view.setVisibility(View.VISIBLE);
        } else {
            cover_view.setVisibility(View.GONE);
        }

    }


    class PoResultListAdapter extends BaseAdapter {

        Drawable arrow;
        Drawable arrow_down;

        PoResultListAdapter() {
            arrow = getResources().getDrawable(R.drawable.ic_menu_arrow_down);
            arrow_down = getResources().getDrawable(R.drawable.ic_menu_arrow_up);
            arrow.setBounds(0, 0, arrow.getMinimumWidth(), arrow.getMinimumHeight());
            arrow_down.setBounds(0, 0, arrow_down.getMinimumWidth(), arrow_down.getMinimumHeight());
        }

        @Override
        public int getCount() {
            return optimizeMethod.size();
        }

        @Override
        public OptimizedMatch getItem(int position) {
            return optimizeMethod.get(position);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertview, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (convertview == null) {
                holder = new ViewHolder();
                convertview = View.inflate(getContext(), R.layout.item_po, null);
                holder.chuan = convertview.findViewById(R.id.item_po_chuanguan);
                holder.content = convertview.findViewById(R.id.item_po_team);
                holder.muti = convertview.findViewById(R.id.item_po_zhushu);

                holder.prize = convertview.findViewById(R.id.item_po_prize);
                holder.ll_detail = convertview.findViewById(R.id.item_po_detail);
                holder.ll_items = convertview.findViewById(R.id.team_items);
                convertview.setTag(holder);
            } else {
                holder = (ViewHolder) convertview.getTag();
            }
            VisibleLl vl = visbleMaps.get(position + "");
            if (vl == null) {
                vl = new VisibleLl();
                vl.visible = holder.ll_detail.getVisibility();
                visbleMaps.put(position + "", vl);
            }
            OptimizedMatch opitemSelect = getItem(position);
            holder.muti.setASBg(R.drawable.roll_as);
            holder.muti.setUnit(1);
            holder.muti.setMin(1);
            holder.muti.setTag(opitemSelect);
            holder.muti.setOnValueChangedLisener(null);
            holder.muti.setRightText("注");
            holder.muti.setValue(opitemSelect.getMultiple() + "");
            holder.muti.setValueColor(R.color.color_111111);
            holder.muti.setOnValueChangedLisener(onItemValueChangedLisener);
            holder.chuan.setText(opitemSelect.getItemSelects().size() + "串1");

            holder.content.setText(opitemSelect.getTeams());
            holder.content.setTag(holder.ll_detail);
            holder.content.setTag(R.id.tag_line, position);
            holder.ll_detail.setVisibility(vl.visible);
            if (vl.visible == View.GONE) {
                holder.content.setCompoundDrawables(null, null, null, arrow);
            } else {
                holder.content.setCompoundDrawables(null, null, null, arrow_down);
            }
            holder.content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LinearLayout ll = (LinearLayout) view.getTag();
                    String postion = view.getTag(R.id.tag_line).toString();
                    VisibleLl vl = visbleMaps.get(postion);
                    if (ll.getVisibility() != View.VISIBLE) {
                        ll.setVisibility(View.VISIBLE);
                    } else {
                        ll.setVisibility(View.GONE);
                    }
                    vl.visible = ll.getVisibility();
                    visbleMaps.put(postion, vl);
                    poResultListAdapter.notifyDataSetChanged();
                }
            });
            holder.prize.setText(opitemSelect.getTotalPrize().setScale(2, BigDecimal.ROUND_HALF_DOWN) + "");
            holder.ll_items.removeAllViews();
            for (ItemSelect itemSelect : opitemSelect.getItemSelects()) {
                View view = View.inflate(getContext(), R.layout.item_po_team, null);
                TextView id = view.findViewById(R.id.item_po_team_id);
                TextView home = view.findViewById(R.id.item_po_team_home);
                TextView away = view.findViewById(R.id.item_po_team_away);
                TextView content = view.findViewById(R.id.item_po_team_content);
                String matchno = itemSelect.getMatchno();
                id.setText(itemSelect.getWeek() + matchno.substring(matchno.length() - 3));
                home.setText(itemSelect.getHomeTeam());
                away.setText(itemSelect.getAwayTeam());
                content.setText(JCCodeTranslate.getInstance().getCodeTranslate(itemSelect.getKey()) + " " + itemSelect.getSpvalue());
                holder.ll_items.addView(view);
            }
            return convertview;
        }

        class ViewHolder {
            TextView chuan;
            TextView content;
            POASView muti;
            TextView prize;
            LinearLayout ll_detail;
            LinearLayout ll_items;
        }
    }

    POASView.OnValueChangedLisener onItemValueChangedLisener = new POASView.OnValueChangedLisener() {
        @Override
        public void onValueChanged(POASView view, int value) {
            int muti_i = value;
            OptimizedMatch opitemSelect = (OptimizedMatch) view.getTag();
            opitemSelect.setMultiple(muti_i);
            opitemSelect.setTotalPrize(opitemSelect.getSinglePrize().multiply(new BigDecimal(muti_i)));
            poResultListAdapter.notifyDataSetChanged();
            setMoneyView();

        }
    };

    POASView.OnValueChangedLisener onValueChangedLisener = new POASView.OnValueChangedLisener() {
        @Override
        public void onValueChanged(POASView view, int value) {
            zhushu = value / view.getUnit();
            prizeo();
            setMoneyViewNoItemChange();
        }
    };
    POASView.OnValueChangedLisener onMutiValueChangedLisener = new POASView.OnValueChangedLisener() {
        @Override
        public void onValueChanged(POASView view, int value) {
            muti = value;
            prizeo();
            setMoneyViewNoItemChange();
        }
    };

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.po_instruction:
                Intent intent = new Intent(getContext(), InfoDetailActivity.class);
                intent.putExtra(IntentParamUtils.TITLE, App.getContext().getString(R.string.po_instruction));
                intent.putExtra(IntentParamUtils.URL, HelpUrlConstants.Help_PO);
                startActivity(intent);
                break;
            case R.id.cover_view:
                showToast("正在进行奖金优化请稍候……");
                break;
        }
    }

    private BigDecimal prize;

    private void setMoneyView() {
        int money = 0;
        for (OptimizedMatch opitemSelect : optimizeMethod) {
            money += (opitemSelect.getMultiple() * 2);
        }
        asv_money.setOnValueChangedLisener(null);
        asv_money.setValue(money);
        asv_money.setOnValueChangedLisener(onValueChangedLisener);
        setPrizeAndAmtView();
        amount = money * muti;
        zhushu = money / 2;
        txt_amount.setText("总彩金 " + amount + " 米");
    }

    private void setMoneyViewNoItemChange() {
        amount = zhushu * muti * 2;
        txt_amount.setText("总彩金 " + amount + " 米");
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle data = msg.getData();
            String min = data.getString("prizeMin");
            BigDecimal prizeMin = new BigDecimal(min);

            String infoStr = "预计奖金：<font color='#fd4a4a'>" + prizeMin.multiply(new BigDecimal(muti)).setScale(2, BigDecimal
                    .ROUND_HALF_DOWN) + "~" + prize.multiply(new
                    BigDecimal
                    (muti)).setScale(2, BigDecimal
                    .ROUND_HALF_DOWN) + "</font>米";

            btm_leftMoney.setText(Html.fromHtml(infoStr));

            expectedPrize = prizeMin.multiply(new BigDecimal(muti)).setScale(2, BigDecimal.ROUND_HALF_DOWN)
                    + "~"
                    + prize.multiply(new BigDecimal(muti)).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        }
    };

    private void setPrizeAndAmtView() {
        prize = new BigDecimal(0);
        handler.post(new Runnable() {
            @Override
            public void run() {
                BigDecimal prizeMin = new BigDecimal(0);
                for (OptimizedMatch opitemSelect : optimizeMethod) {
                    prize = prize.add(opitemSelect.getTotalPrize());
                    if (prizeMin.equals(new BigDecimal(0))) {
                        prizeMin = prize;
                    } else {
                        if (prizeMin.compareTo(opitemSelect.getTotalPrize()) > 0) {
                            prizeMin = opitemSelect.getTotalPrize();
                        }
                    }
                }
                ArrayList<OptimizedMatch> cloneList = JZGroupUtil.clone(optimizeMethod);
                Collections.sort(cloneList, new Comparator<OptimizedMatch>() {
                    @Override
                    public int compare(OptimizedMatch optimizedMatch, OptimizedMatch t1) {
                        return t1.getTotalPrize().compareTo(optimizedMatch.getTotalPrize());
                    }
                });
                prizeList.clear();
                getPrizeList(cloneList);
                Collections.sort(prizeList);
                prize = prizeList.get(prizeList.size() - 1);
                Bundle bundle = new Bundle();
                bundle.putString("prizeMin", prizeMin.toString());
                Message message = new Message();
                message.setData(bundle);
                handler.sendMessage(message);
            }
        });

    }

    private List<BigDecimal> prizeList = new ArrayList<>();

    private void getPrizeList(ArrayList<OptimizedMatch> optimizeMethod) {
        OptimizedMatch optimizedMatch = optimizeMethod.get(0);
        BigDecimal prize = optimizedMatch.getTotalPrize();
        ArrayList<ItemSelect> itemSelects = optimizedMatch.getItemSelects();
        ArrayList<OptimizedMatch> noChooseList = new ArrayList<>();
        a:
        for (int i = 1; i <= optimizeMethod.size() - 1; i++) {
            List<ItemSelect> selects = optimizeMethod.get(i).getItemSelects();
            boolean isAdd = false;
            b:
            for (ItemSelect select : selects) {
                int handicap = Integer.parseInt(select.getHandicap().replace("+", ""));
                for (ItemSelect itemSelect : itemSelects) {
                    if (select.getHomeTeam().equals(itemSelect.getHomeTeam()) && !select.getKey().equals(itemSelect.getKey())) {
                        if (canAdd(select, itemSelect, handicap)) {
                            isAdd = true;
                        } else {
                            isAdd = false;
                            break b;
                        }
                    } else {
                        isAdd = true;
                    }
                }
            }
            if (isAdd) {
                itemSelects.addAll(optimizeMethod.get(i).getItemSelects());
                prize = prize.add(optimizeMethod.get(i).getTotalPrize());
            } else {
                noChooseList.add(optimizeMethod.get(i));
            }
        }
        prizeList.add(prize);
        if (!noChooseList.isEmpty()) {
            if (noChooseList.size() == 1) {
                prizeList.add(noChooseList.get(0).getTotalPrize());
            } else {
                getPrizeList(noChooseList);
            }
        }
    }

    private boolean canAdd(ItemSelect select, ItemSelect itemSelect, int handicap) {
        switch (lotteryId) {
            case LotteryIDUtils.JZSPFRSPF_LOTTERY_ID_INT:
                return setSPF(select, itemSelect, handicap);
            case LotteryIDUtils.JZHH_LOTTERY_ID_INT:
                JCInfo jcInfo = null;
                for (int i = 0; i < infos.size(); i++) {
                    if (infos.get(i).getHomeTeam().equals(select.getHomeTeam())) {
                        jcInfo = infos.get(i);
                        break;
                    }
                }
                if (jcInfo == null) {
                    return false;
                }
                JZHGGroup jzhhGroup = new JZHGGroup(jcInfo);
                ArrayList<StringArrayObj> allHH = jzhhGroup.getAll();
                for (StringArrayObj stringArrayObj : allHH) {
                    String[] tagArr = stringArrayObj.tagArr;
                    boolean hasF = false, hasS = false;
                    for (String s : tagArr) {
                        if (select.getKey().equals(s)) {
                            hasF = true;
                        }
                        if (itemSelect.getKey().equals(s)) {
                            hasS = true;
                        }
                    }
                    if (hasF && hasS) {
                        return true;
                    }
                }
                return false;
            default:
                return false;
        }
    }

    private boolean setSPF(ItemSelect select, ItemSelect itemSelect, int handicap) {
        if (select.getKey().equals("vs_3")) {
            if (handicap == -1) {
                if (itemSelect.getKey().equals("letvs_3") || itemSelect.getKey().equals("letvs_1")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap < -1) {
                if (itemSelect.getKey().equals("letvs_3") || itemSelect.getKey().equals("letvs_1") || itemSelect.getKey().equals("letvs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap == 1) {
                if (itemSelect.getKey().equals("letvs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap > 1) {
                if (itemSelect.getKey().equals("letvs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (select.getKey().equals("vs_1")) {
            if (handicap == -1) {
                if (itemSelect.getKey().equals("letvs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap < -1) {
                if (itemSelect.getKey().equals("letvs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap == 1) {
                if (itemSelect.getKey().equals("letvs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap > 1) {
                if (itemSelect.getKey().equals("letvs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (select.getKey().equals("vs_0")) {
            if (handicap == -1) {
                if (itemSelect.getKey().equals("letvs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap < -1) {
                if (itemSelect.getKey().equals("letvs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap == 1) {
                if (itemSelect.getKey().equals("letvs_1") || itemSelect.getKey().equals("letvs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap > 1) {
                if (itemSelect.getKey().equals("letvs_3") || itemSelect.getKey().equals("letvs_1") || itemSelect.getKey().equals("letvs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (select.getKey().equals("letvs_3")) {
            if (handicap == -1) {
                if (itemSelect.getKey().equals("vs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap < -1) {
                if (itemSelect.getKey().equals("vs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap == 1) {
                if (itemSelect.getKey().equals("vs_3") || itemSelect.getKey().equals("vs_1")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap > 1) {
                if (itemSelect.getKey().equals("vs_3") || itemSelect.getKey().equals("vs_1") || itemSelect.getKey().equals("vs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (select.getKey().equals("letvs_1")) {
            if (handicap == -1) {
                if (itemSelect.getKey().equals("vs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap < -1) {
                if (itemSelect.getKey().equals("vs_3")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap == 1) {
                if (itemSelect.getKey().equals("vs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap > 1) {
                if (itemSelect.getKey().equals("vs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (select.getKey().equals("letvs_0")) {
            if (handicap == -1) {
                if (itemSelect.getKey().equals("vs_1") || itemSelect.getKey().equals("vs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap < -1) {
                if (itemSelect.getKey().equals("vs_3") || itemSelect.getKey().equals("vs_1") || itemSelect.getKey().equals("vs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap == 1) {
                if (itemSelect.getKey().equals("vs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else if (handicap > 1) {
                if (itemSelect.getKey().equals("vs_1") || itemSelect.getKey().equals("vs_0")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public void setTitleStr() {
        top_title.setText("奖金优化");
    }

    @Override
    public boolean isShowStartBask() {
        return false;
    }

    public int getLotteryId() {
        if (lotteryId == LotteryIDUtils.JZSPFRSPF_LOTTERY_ID_INT) {
            String VS_0 = "vs_0";
            String VS_1 = "vs_1";
            String VS_3 = "vs_3";
            boolean hasSPF = false;
            boolean hasRSPF = false;
            for (OptimizedMatch opitemSelect : optimizeMethod) {
                for (ItemSelect itemSelect : opitemSelect.getItemSelects()) {
                    if (itemSelect.getKey().equals(VS_0) || itemSelect.getKey().equals(VS_1) || itemSelect.getKey().equals(VS_3)) {
                        hasSPF = true;
                    } else {
                        hasRSPF = true;
                    }
                }
            }
            if (hasSPF && hasRSPF) {
                return LotteryIDUtils.JZHH_LOTTERY_ID_INT;
            } else if (hasSPF) {
                return LotteryIDUtils.JZSPF_LOTTERY_ID_INT;
            } else {
                return LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT;
            }
        }
        return lotteryId;
    }

    public int getLotteryId(OptimizedMatch opitemSelect) {
        if (lotteryId == LotteryIDUtils.JZSPFRSPF_LOTTERY_ID_INT) {
            String VS_0 = "vs_0";
            String VS_1 = "vs_1";
            String VS_3 = "vs_3";
            boolean hasSPF = false;
            boolean hasRSPF = false;
            for (ItemSelect itemSelect : opitemSelect.getItemSelects()) {
                if (itemSelect.getKey().equals(VS_0) || itemSelect.getKey().equals(VS_1) || itemSelect.getKey().equals(VS_3)) {
                    hasSPF = true;
                } else {
                    hasRSPF = true;
                }
            }
            if (hasSPF && hasRSPF) {
                return LotteryIDUtils.JZHH_LOTTERY_ID_INT;
            } else if (hasSPF) {
                return LotteryIDUtils.JZSPF_LOTTERY_ID_INT;
            } else {
                return LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT;
            }
        }
        return lotteryId;
    }

    @Override
    public void setBtmView() {

    }

    @Override
    protected void adapterChange() {

    }


    @Override
    protected void clear() {
        JC_GuoGuanDialog.result = JC_GuoGuanDialog.GG_RESULT;
//        tv_guoGuanStyle.setText(JC_GuoGuanDialog.GG_RESULT);
//        initGuoGuanView();
        clearData();
    }

    private void clearData() {
        for (int i = 0; i < infos.size(); i++) {
            JCInfo info = infos.get(i);
            if (info.getOnclikNum() > 0) {
                info.setOnclikNum(0);
                info.setHg_more(0);
                info.setDan(false);
                Iterator<String> keys = info.btnHGPojo.keySet().iterator();
                while (keys.hasNext()) {
                    info.btnHGPojo.get(keys.next()).isSel = false;
                }

            }
            List<ZCBF_infor> touZhu = info.getTouZhuInfor();
            for (int j = 0; j < touZhu.size(); j++) {
                touZhu.get(j).setSelect(false);
            }
            info.setResult("");
        }
    }


    @Override
    public int getOneAmount() {
        return 2;
    }

    @Override
    public int getZhushu() {
        return zhushu;
    }

    @Override
    public String isPrizeOptimize() {
        return "1";
    }

    @Override
    public int getBeishu() {
        return muti;
    }

    @Override
    public long getAmount() {
        return amount * 100;
    }

    @Override
    protected String getContent() {
        StringBuffer betcode = new StringBuffer();
        int lotteryId = getLotteryId();
        for (OptimizedMatch opitemSelect : optimizeMethod) {
            betcode.append(lotteryId);
            betcode.append("1");
            betcode.append(opitemSelect.getItemSelects().size() + "001");
            betcode.append("-");
            for (ItemSelect itemSelect : opitemSelect.getItemSelects()) {
                betcode.append(itemSelect.getMatchno());
                if (lotteryId == LotteryIDUtils.JZHH_LOTTERY_ID_INT) {
                    betcode.append("*");
                    betcode.append(JCCodeTranslate.getInstance().getItemCode(itemSelect.getKey()));
                }
                betcode.append("(");
                betcode.append(itemSelect.getItem());
                betcode.append(")");
                betcode.append("|");
            }
            betcode = betcode.deleteCharAt(betcode.length() - 1);
            betcode.append("^:");
            betcode.append(opitemSelect.getMultiple());
            betcode.append(":");
            betcode.append(opitemSelect.getMultiple() * 200);
            betcode.append("!");
        }
        return betcode.deleteCharAt(betcode.length() - 1).toString();
    }

    @Override
    public void goBet() {
        if (poing) {
            showToast("正在进行奖金优化请稍等……");
        } else {
            bet();
        }
    }

}
